package moh.s2.web.controller;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import moh.s2.s2Facade.PropertiesFacade;
import moh.s2.s2entity.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/c2")
public class PropertiesController {

	@Autowired
	private PropertiesFacade propertiesFacade;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String listAll(Model model, HttpServletRequest req,
			HttpServletResponse resp) {

		List<Properties> props = null;
		try {
			props = propertiesFacade.list();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("props", props);
		return "main";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("prop", new Properties());
		return "addproperties";
	}

	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public String update(@ModelAttribute("prop") Properties prop,
			BindingResult br, Model model) {
		model.addAttribute("prop", prop);
		return "addproperties";
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(@ModelAttribute("prop") Properties prop,
			BindingResult br, Model model) {
		if (br.hasErrors()) {
			return "addproperties";
		}
		if (prop.getId() != null) {
			try {
				propertiesFacade.update(prop);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			try {
				propertiesFacade.add(prop);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "redirect:list";
	}

	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	public String delete(@ModelAttribute("prop") Properties prop,
			BindingResult br, Model model) {
		if (br.hasErrors()) {
			return "redirect:list";
		}
		if (prop.getId() != null) {
			try {
				propertiesFacade.delete(prop);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
		return "redirect:list";
	}
	
	
	@PostConstruct
	public void init() {
		try {
			propertiesFacade.list();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public PropertiesFacade getPropertiesFacade() {
		return propertiesFacade;
	}

	public void setPropertiesFacade(PropertiesFacade propertiesFacade) {
		this.propertiesFacade = propertiesFacade;
	}
}
